package com.example.memolaga.password;

import android.util.Base64;

import java.math.BigInteger;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by matteolappa on 24/06/2013.
 */
public class FunctionsPass {

    private static final String STR_AES_CYPHER = "AES/ECB/PKCS5padding";
    private static final String STR_AES_PBKDF = "PBKDF2WithHmacSHA1";
    private static final String STR_AES_INSTANCE ="AES";

    private static final String STR_AES_SALT ="asdfgjklòzxcmnvb";

    private static final String STR_SHA_INSTANCE = "SHA-256";

    private static FunctionsPass singleton = null;

    private FunctionsPass(){}

    public static FunctionsPass getInstance()
    {
        if (singleton == null)
            singleton = new FunctionsPass();
        return singleton;
    }

    public String getHashSHA256(String pass)
    {//Versione che implementa le due funzioni sopra
        MessageDigest digest=null;
        try {
            digest = MessageDigest.getInstance(STR_SHA_INSTANCE);
        } catch (NoSuchAlgorithmException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        digest.reset();
        byte[] data=digest.digest(pass.getBytes());
        return String.format("%0" + (data.length*2) + "X", new BigInteger(1, data));
    }

    public boolean checkPass(String pass1, String pass2)
    {
        //return pass1.equalsIgnoreCase(pass2);
        return pass1.equals(pass2);
    }

    //code from http://myandroidsolutions.blogspot.it/2013/02/aes-cb-encryption-in-android-using-key.html
    public static String decryptString(String key, String textToDecrypt) {
        String ret = "error";
        byte[] rawKey = new byte[32];
        java.util.Arrays.fill(rawKey, (byte) 0);
        byte[] keyOk = hmacSha1(key);
        for (int i = 0; i < keyOk.length; i++) {
            rawKey[i] = keyOk[i];
        }
        SecretKeySpec skeySpec = new SecretKeySpec(hmacSha1(key), STR_AES_INSTANCE);

        try {
            Cipher cipher = Cipher.getInstance(STR_AES_CYPHER);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encryptedData = cipher.doFinal(Base64.decode(textToDecrypt, Base64.NO_CLOSE));

            if (encryptedData != null) ret =new String(encryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ret;
    }

    // encryption method
    public static String encryptString(String key, String clearText) {
        String ret = "error";
        byte[] rawKey = new byte[32];
        java.util.Arrays.fill(rawKey, (byte) 0);

        byte[] keyOk = hmacSha1(key);
        for (int i = 0; i < keyOk.length; i++) {
            rawKey[i] = keyOk[i];
        }

        SecretKeySpec skeySpec = new SecretKeySpec(hmacSha1(key), STR_AES_INSTANCE);

        try {
            Cipher cipher = Cipher.getInstance(STR_AES_CYPHER);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encryptedData = cipher.doFinal(clearText.getBytes());

            if (encryptedData != null) ret = Base64.encodeToString(encryptedData, Base64.NO_CLOSE);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ret;

    }

    // key generator method
    public static byte[] hmacSha1(String key) {

        SecretKeyFactory factory = null;
        Key keyByte = null;

        try {
            factory = SecretKeyFactory.getInstance(STR_AES_PBKDF);
            KeySpec keyspec = new PBEKeySpec(key.toCharArray(), STR_AES_SALT.getBytes(), 1024, 256);
            keyByte = factory.generateSecret(keyspec);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }
        return keyByte.getEncoded();
    }


}


